package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    //测试回退
    //熊翔

//
    @Reference
    SetmealService setmealService;

    @Autowired
    JedisPool jedisPool;

    //上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = "LTAI4Fq78KrCKJyZArQXunpg";
        String accessKeySecret = "z8A7PvELuyHrou7iiCTqJXCQTInm6I";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 上传Byte数组。
        try {
            //获取Bttes数组
            byte[] bytes = imgFile.getBytes();
            //获取文件后缀
            String suffix = imgFile.getOriginalFilename().substring(imgFile.getOriginalFilename().lastIndexOf(".") + 1);
            //生成一个随机数
            String s = UUID.randomUUID().toString();
            //拼接文件名称
            String name = s + "." + suffix;
            ossClient.putObject("xx0034", name, new ByteArrayInputStream(bytes));
            // 关闭OSSClient。
            ossClient.shutdown();
            //将上传文件名保存至redis
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,name);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, name);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
        //添加检查套餐
        @RequestMapping(value = "/add",method = RequestMethod.POST)
        public Result add(@RequestBody Setmeal setmeal ,Integer[] checkgroupIds){
          return  setmealService.add(checkgroupIds,setmeal);
        }
        //分页查询
        @RequestMapping(value = "/findPage",method = RequestMethod.POST)
        public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
          return  setmealService.findByPage(queryPageBean);
        }
        //通过id查询
        @RequestMapping(value = "/findById/{id}",method = RequestMethod.GET)
        public Result findById(@PathVariable Integer id){
            return  setmealService.findById(id);
        }
        //通过id查询中间表信息
        @RequestMapping(value = "/findSetmealIdByCheckgroup/{id}",method = RequestMethod.GET)
        public Result findSetmealIdByCheckgroup(@PathVariable Integer id){
            return  setmealService.findBySp(id);
        }
        //通过id修改
        @RequestMapping(value = "/update",method = RequestMethod.POST)
        public Result update(Integer[] checkgroupIds,@RequestBody Setmeal setmeal){
            return  setmealService.update(checkgroupIds,setmeal);
        }

    }

    //测试回退
    //测试回退2



//123
